﻿using UnityEngine;

namespace SoarD.Pandora.Console
{
    public partial class PdrConsoleWindow
    {
        private static float TitleWidth = 240f;
        public abstract class ScrollWindowBase : IConsoleWindow
        {
            private Vector2 m_ScrollPosition = Vector2.zero;

            public virtual void Initialize(params object[] args)
            {

            }

            public virtual void Shutdown()
            {

            }

            public virtual void OnEnter()
            {

            }

            public virtual void OnLeave()
            {

            }

            public virtual void OnUpdate(float elapseSeconds, float realElapseSeconds)
            {

            }

            private Vector2 oldInfoDrag;
            public void OnDraw()
            {
                getDownPos();

                Vector2 drag = getDrag();
                if ((drag.x != 0) && (downPos != Vector2.zero))
                {
                    m_ScrollPosition.x -= (drag.x - oldInfoDrag.x);
                }
                if ((drag.y != 0) && (downPos != Vector2.zero))
                {
                    m_ScrollPosition.y += (drag.y - oldInfoDrag.y);
                }
                oldInfoDrag = drag;
                m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition);
                {
                    OnDrawScrollableWindow();
                    GUILayout.Space(spaceHeight*2);
                }
                GUILayout.EndScrollView();
            }

            protected abstract void OnDrawScrollableWindow();

            protected void DrawItem(string title, string content)
            {
                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label(title, GUILayout.Width(TitleWidth));
                    GUILayout.Label(content);
                }
                GUILayout.EndHorizontal();
            }


            Vector2 downPos;
            void getDownPos()
            {
                if (Application.platform == RuntimePlatform.Android ||
                   Application.platform == RuntimePlatform.IPhonePlayer)
                {

                    if (Input.touches.Length == 1 && Input.touches[0].phase == TouchPhase.Began)
                    {
                        downPos = Input.touches[0].position;
                    }
                }
                else
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        downPos.x = Input.mousePosition.x;
                        downPos.y = Input.mousePosition.y;
                    }
                }
            }

            Vector2 mousePosition;
            Vector2 getDrag()
            {
                if (Application.platform == RuntimePlatform.Android ||
                    Application.platform == RuntimePlatform.IPhonePlayer)
                {
                    if (Input.touches.Length != 1)
                    {
                        return Vector2.zero;
                    }
                    return Input.touches[0].position - downPos;
                }
                else
                {
                    if (Input.GetMouseButton(0))
                    {
                        mousePosition = Input.mousePosition;
                        return mousePosition - downPos;
                    }
                    else
                    {
                        return Vector2.zero;
                    }
                }
            }
        }
    }
}